package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerTransferHistoryDTO;
import com.prd.mapper.CustomerTransferHistoryMapper;
import com.prd.model.CustomerTransferHistoryDO;
import com.prd.service.CustomerTransferHistoryService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户-移交历史总表 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-12-05
 */
@Service("CustomerTransferHistoryService")
public class CustomerTransferHistoryServiceImpl extends ServiceImpl<CustomerTransferHistoryMapper, CustomerTransferHistoryDO> implements CustomerTransferHistoryService {

    @Autowired
    private CustomerTransferHistoryMapper transferHistoryMapper;

    @Override
    public JsonData uAdd(CustomerTransferHistoryDTO transferHistoryDTO) {
        if (transferHistoryDTO.getCustomerNo() == null || transferHistoryDTO.getProjectId() == -1) {
            return JsonData.buildError("客户编号/项目id不可为空");
        }
        CustomerTransferHistoryDO transferHistoryDO = new CustomerTransferHistoryDO();
        CopyNonNullBeanUtils.copyProperties(transferHistoryDTO, transferHistoryDO);
        transferHistoryMapper.insert(transferHistoryDO);
        return JsonData.buildSuccess(transferHistoryDO);
    }

    @Override
    public List<CustomerTransferHistoryDTO> uSearch(String customerNo, int projectId) {
        LambdaQueryWrapper<CustomerTransferHistoryDO> wrapper = new LambdaQueryWrapper<CustomerTransferHistoryDO>();
        if (customerNo != null)
            wrapper.eq(CustomerTransferHistoryDO::getCustomerNo, customerNo);
        if (projectId != -1)
            wrapper.eq(CustomerTransferHistoryDO::getProjectId, projectId);
        List<CustomerTransferHistoryDO> customerTransferHistoryDOList = transferHistoryMapper.selectList(wrapper);
        return customerTransferHistoryDOList.stream().map(this::doToDto).collect(Collectors.toList());
    }

    private CustomerTransferHistoryDTO doToDto(CustomerTransferHistoryDO customerTransferHistoryDO) {
        CustomerTransferHistoryDTO transferHistoryDTO = new CustomerTransferHistoryDTO();
        BeanUtils.copyProperties(customerTransferHistoryDO, transferHistoryDTO);
        return transferHistoryDTO;
    }
}
